package com.autonavi.yunda.yunji.common.utils.geo;

import com.autonavi.yunda.yunji.common.exception.AmapException;
import com.autonavi.yunda.yunji.common.exception.AmapExceptionCode;
import org.apache.commons.lang3.StringUtils;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.GeometryFactory;
import org.locationtech.jts.geom.PrecisionModel;
import org.locationtech.jts.io.WKTReader;
import org.locationtech.jts.simplify.TopologyPreservingSimplifier;
import org.wololo.geojson.GeoJSON;


/**
 * 简化wkt
 */
public class SimplifyWkt {

    private static final int SMALL_RAITO_SIMPLIFY_LIMIT = 5000;
    private static final int SIMPLIFY_LIMIT = 500;
    private static final double SIMPLIFY_RATIO = 0.008;

    /**
     * 更小的抽稀比率,保留更多点位
     */
    private static final double SIMPLIFY_RATIO_SMALL = 0.0005;


    public static GeoJSON simplifyMultiPolygonWktFunc(GeoJSON geoJSON) {
        try {
            String wkt = GeoUtil.geoJSONtoWkt(geoJSON);
            String result = simplifyMultiPolygonWktFunc(wkt);
            return GeoUtil.wktToGeoJSON(result);
        } catch (Exception e) {
            throw new AmapException(AmapExceptionCode.COMMON_ILLEGAL_PARAM, "wkt围栏抽稀异常", e);
        }
    }

    public static String simplifyMultiPolygonWktFunc(String wkt) {
        try {
            String result = simplifyWktFunc(wkt, null);
            if (result.startsWith("POLYGON")) {
                result = result.replaceAll("\\(\\(", "(((")
                        .replaceAll("\\)\\)", ")))")
                        .replaceAll("POLYGON", "MULTIPOLYGON");
            }
            System.out.println("result length:" + result.split(", ").length);
            return result;
        } catch (Exception e) {
            throw new AmapException(AmapExceptionCode.COMMON_ILLEGAL_PARAM, "wkt围栏抽稀异常", e);
        }
    }

    /**
     * wkt 抽稀
     */
    public static String simplifyWktFunc(String wkt, String ratio) throws Exception {
        return simplifyWktFunc(wkt,ratio,SIMPLIFY_LIMIT);
    }

    /**
     * wkt 抽稀 ： 支持设置阈值大小
     */
    public static String simplifyWktFunc(String wkt, String ratio,int simplifyLimit) throws Exception {
        if (StringUtils.isBlank(wkt)) {
            return wkt;
        }
        double ratioReal = StringUtils.isNotBlank(ratio) ? Double.parseDouble(ratio) : SIMPLIFY_RATIO;
        int length = wkt.split(",").length;
        System.out.println("length:" + length);
        if (length > simplifyLimit && length < SMALL_RAITO_SIMPLIFY_LIMIT) {
            ratioReal = SIMPLIFY_RATIO_SMALL;
        }
        if (length <= simplifyLimit) {
            return wkt;
        } else {
            Geometry newWkt = simplifyTP(ratioReal, wkt);
            return newWkt.toString();
        }
    }

    /**
     * 简化wkt
     */
    public static Geometry simplifyTP(double distance, String wkt) throws Exception {
        GeometryFactory geometryFactory = new GeometryFactory(new PrecisionModel(PrecisionModel.FLOATING), 4326);
        WKTReader wktReader = new WKTReader(geometryFactory);
        Geometry g;
        if (wkt.contains("MULTIPOLYGON")) {
            g = wktReader.read(wkt);
        } else {
            g = wktReader.read("MULTIPOLYGON (((" + wkt + ")))");
        }
        return TopologyPreservingSimplifier.simplify(g, distance);
    }


    public static void main(String[] args) throws Exception {
        //朝阳区
        String wkt = "MULTIPOLYGON(((116.44364 39.872846,116.443926 39.874416,116.444117 39.875362,116.444256 39.875752,116.444463 39.876156,116.445373 39.877795,116.445538 39.878207,116.445616 39.878576,116.445642 39.878901,116.445642 39.879279,116.445339 39.881003,116.444736 39.884684,116.44473 39.884709,116.444727 39.884736,116.444707 39.884858,116.444179 39.889017,116.444067 39.890037,116.445964 39.890147,116.445903 39.891519,116.446934 39.891517,116.446986 39.891205,116.447359 39.891152,116.447393 39.890804,116.448199 39.890712,116.448311 39.890504,116.450017 39.890449,116.450381 39.890244,116.450898 39.890243,116.450859 39.89056,116.45074 39.89133,116.45066 39.894094,116.44716 39.894186,116.447004 39.897059,116.446831 39.89746,116.446805 39.898098,116.446857 39.898341,116.446833 39.899542,116.446808 39.899577,116.446831 39.899619,116.446823 39.900042,116.445965 39.900061,116.445879 39.901034,116.448027 39.901234,116.449066 39.90125,116.44872 39.903242,116.447023 39.903159,116.446538 39.903151,116.446087 39.903151,116.444684 39.903214,116.443809 39.903224,116.443064 39.903182,116.441963 39.903097,116.441574 39.903019,116.440543 39.902721,116.440196 39.902661,116.439875 39.902644,116.438697 39.902594,116.437995 39.902612,116.437666 39.902578,116.437431 39.902491,116.43718 39.902331,116.43679 39.902014,116.436478 39.902046,116.436478 39.902197,116.436453 39.902618,116.436323 39.90363,116.435959 39.905536,116.435838 39.906838,116.435751 39.908227,116.435673 39.910515,116.435648 39.910901,116.435569 39.911326,116.435084 39.913358,116.435041 39.913641,116.434989 39.913958,116.434911 39.915273,116.434808 39.917226,116.434652 39.921168,116.434488 39.924518,116.434309 39.928686,116.434953 39.928666,116.435486 39.92865,116.435941 39.928653,116.435944 39.928449,116.437057 39.928446,116.437023 39.928827,116.436955 39.929183,116.436935 39.929359,116.436931 39.929405,116.437063 39.929419,116.43731 39.929401,116.437333 39.929258,116.43731 39.928966,116.437581 39.928961,116.43759 39.928701,116.438727 39.928704,116.43873 39.928526,116.43903 39.928532,116.439035 39.929442,116.439059 39.929538,116.439572 39.929704,116.440306 39.929715,116.440329 39.929547,116.440417 39.928678,116.441099 39.928647,116.441723 39.92863,116.443553 39.928673,116.443634 39.92869,116.443682 39.928669,116.443714 39.930897,116.443707 39.930969,116.443716 39.931042,116.443722 39.931423,116.443722 39.931443,116.443723 39.931473,116.443752 39.933526,116.443751 39.933607,116.443751 39.933671,116.443759 39.933781,116.443747 39.933882,116.443709 39.936668,116.443753 39.936924,116.444203 39.937887,116.444455 39.938421,116.444496 39.938649,116.444482 39.938675,116.444466 39.938698,116.444541 39.939154,116.444541 39.939883,116.444548 39.939969,116.444552 39.940022,116.444557 39.940084,116.444611 39.94079,116.444689 39.941237,116.444784 39.941623,116.444949 39.941827,116.446057 39.942416,116.446179 39.94248,116.4463 39.942567,116.446387 39.942658,116.446439 39.942749,116.446473 39.942793,116.446551 39.94297,116.446838 39.943816,116.447158 39.944762,116.447236 39.944918,116.447271 39.945026,116.447271 39.945157,116.44721 39.945282,116.447063 39.945478,116.446933 39.945721,116.446838 39.945986,116.446708 39.946112,116.446561 39.946169,116.446344 39.9462,116.444672 39.946246,116.444334 39.946247,116.443841 39.946226,116.443641 39.946209,116.443538 39.946178,116.442645 39.945706,116.442576 39.945659,116.442506 39.945585,116.442272 39.945299,116.442117 39.945143,116.442021 39.945078,116.441909 39.945044,116.44177 39.945035,116.441588 39.945057,116.441276 39.945105,116.441059 39.945149,116.440799 39.945228,116.440557 39.945301,116.440546 39.945357,116.440751 39.945772,116.442325 39.947158,116.442244 39.949701,116.440281 39.949689,116.439951 39.952135,116.439074 39.952166,116.435971 39.952121,116.435887 39.952143,116.435836 39.952119,116.435419 39.952118,116.435417 39.952087,116.435457 39.952037,116.436017 39.950846,116.43574 39.950799,116.436216 39.949817,116.436415 39.949747,116.436797 39.949691,116.436806 39.949387,116.436735 39.949359,116.436701 39.949348,116.436701 39.949248,116.435098 39.949233,116.434821 39.949038,116.434361 39.949068,116.434205 39.949307,116.434206 39.949388,116.434174 39.94942,116.434153 39.949498,116.434101 39.949754,116.433998 39.949937,116.433772 39.950059,116.433486 39.950141,116.433053 39.950177,116.432299 39.950178,116.430211 39.950167,116.430207 39.951061,116.429889 39.951077,116.429226 39.9511,116.429191 39.951715,116.429967 39.951782,116.430243 39.951859,116.431137 39.951903,116.431133 39.952253,116.43026 39.952256,116.430277 39.952392,116.430208 39.952396,116.430122 39.952479,116.430033 39.952828,116.430037 39.953396,116.429546 39.953409,116.429422 39.953413,116.429421 39.953544,116.42939 39.95423,116.429387 39.954281,116.429386 39.954298,116.429357 39.954303,116.429289 39.9543,116.429022 39.954278,116.428996 39.955624,116.429204 39.955649,116.429196 39.956327,116.429161 39.956774,116.429005 39.957269,116.429803 39.957255,116.429829 39.956534,116.430618 39.95656,116.430514 39.959247,116.429585 39.959231,116.429497 39.959216,116.429417 39.959229,116.427408 39.959196,116.427369 39.959175,116.427316 39.959195,116.425433 39.959164,116.425338 39.962085,116.425148 39.962166,116.425094 39.962209,116.425045 39.962208,116.424942 39.962208,116.414208 39.962063,116.4142 39.962178,116.41414 39.963809,116.412187 39.96378,116.412161 39.964943,116.411424 39.96493,116.411231 39.96875,116.411232 39.968779,116.411233 39.968785,116.411227 39.968831,116.411095 39.971454,116.408786 39.971463,116.408821 39.972496,116.409341 39.97253,116.409359 39.974036,116.40751 39.974002,116.407579 39.971654,116.407571 39.97135,116.407545 39.971085,116.407484 39.970824,116.407406 39.970477,116.407362 39.970204,116.407388 39.969661,116.407466 39.967951,116.407483 39.96763,116.407553 39.967352,116.407666 39.966909,116.407718 39.966562,116.407743 39.96608,116.407778 39.965139,116.407873 39.962178,116.405486 39.962256,116.405477 39.96187,116.40447 39.961891,116.404192 39.961891,116.403671 39.961848,116.402682 39.962433,116.401449 39.962424,116.401301 39.962082,116.40045 39.962051,116.399703 39.962389,116.398852 39.96238,116.398739 39.962562,116.398227 39.962532,116.396959 39.963204,116.395179 39.963191,116.395105 39.963218,116.395046 39.963158,116.394926 39.963198,116.394822 39.963195,116.394796 39.963195,116.394675 39.963195,116.39429 39.963183,116.393199 39.963175,116.393126 39.963147,116.393041 39.963174,116.392658 39.963171,116.392514 39.963128,116.392385 39.963169,116.389506 39.963147,116.389479 39.961038,116.387663 39.960923,116.38782 39.961605,116.388011 39.962694,116.387901 39.963061,116.388584 39.963073,116.388572 39.963529,116.389236 39.96354,116.389206 39.963914,116.389277 39.964431,116.388541 39.964431,116.388498 39.96497,116.388232 39.964976,116.388252 39.965784,116.389076 39.965999,116.389694 39.966215,116.390194 39.966263,116.390122 39.968172,116.390081 39.968408,116.391921 39.968453,116.391956 39.968726,116.392347 39.968939,116.393008 39.969157,116.393981 39.969183,116.394163 39.969392,116.394172 39.97006,116.394094 39.972864,116.394093 39.973066,116.387449 39.972917,116.386213 39.972785,116.381205 39.972721,116.38117 39.972732,116.381136 39.972721,116.380912 39.972718,116.381191 39.974728,116.38126 39.975306,116.381304 39.975957,116.381321 39.976478,116.381321 39.976946,116.381304 39.97735,116.381261 39.977684,116.3812 39.977979,116.380757 39.979624,116.379419 39.984249,116.378593 39.987061,116.377794 39.989907,116.377472 39.990944,116.377125 39.991899,116.376986 39.992255,116.376812 39.992576,116.37656 39.99297,116.376334 39.993278,116.375074 39.994934,116.371979 39.998954,116.368267 40.003871,116.36558 40.007517,116.364321 40.009231,116.364253 40.009321,116.364181 40.009418,116.364097 40.009532,116.363119 40.010834,116.362102 40.012195,116.360962 40.013673,116.358241 40.017215,116.35484 40.021597,116.353479 40.023354,116.353475 40.023418,116.353373 40.023491,116.350882 40.026708,116.351579 40.026852,116.352309 40.026975,116.354841 40.027362,116.357807 40.027779,116.359599 40.028004,116.361687 40.028272,116.363183 40.028413,116.363696 40.028483,116.364279 40.028567,116.370462 40.029744,116.371896 40.029945,116.374314 40.030404,116.377234 40.030989,116.378034 40.031116,116.378704 40.031187,116.381746 40.031324,116.387699 40.031699,116.389802 40.031801,116.391096 40.031902,116.39246 40.032089,116.393677 40.032363,116.395093 40.032859,116.394033 40.034682,116.393772 40.034833,116.393434 40.034903,116.392912 40.035158,116.392634 40.035301,116.392461 40.035358,116.392296 40.035375,116.392026 40.03537,116.391879 40.035396,116.391818 40.035418,116.391757 40.035448,116.391705 40.035492,116.39167 40.035522,116.391627 40.035539,116.391557 40.035544,116.391496 40.035556,116.391436 40.0356,116.391349 40.035661,116.391192 40.035747,116.391123 40.035774,116.391045 40.035795,116.390958 40.035821,116.390784 40.035947,116.390593 40.036146,116.390402 40.036372,116.390254 40.036589,116.390185 40.036775,116.390167 40.036953,116.390167 40.037092,116.390211 40.037322,116.39022 40.037383,116.390202 40.037535,116.390211 40.037635,116.390367 40.038112,116.390376 40.038178,116.390359 40.038243,116.390341 40.038308,116.390341 40.038373,116.39035 40.03846,116.390367 40.038611,116.39035 40.038651,116.390307 40.038685,116.390211 40.03872,116.390141 40.03875,116.390011 40.038767,116.389916 40.038807,116.389855 40.038885,116.38982 40.039011,116.38982 40.039149,116.389864 40.039293,116.389985 40.03944,116.390498 40.039823,116.390593 40.039936,116.390654 40.04007,116.390698 40.040266,116.390698 40.040426,116.390646 40.041272,116.390828 40.041316,116.392288 40.041604,116.392965 40.041739,116.393626 40.040715,116.394181 40.039791,116.394251 40.038211,116.394251 40.038137,116.39452 40.037824,116.394746 40.037534,116.395232 40.037039,116.395327 40.036766,116.39564 40.036831,116.396656 40.037075,116.398031 40.037379,116.398076 40.037395,116.398123 40.037399,116.399002 40.037588,116.399114 40.037796,116.399462 40.037662,116.399705 40.037619,116.400226 40.037758,116.401781 40.038106,116.403013 40.038414,116.403925 40.038592,116.405262 40.038974,116.405566 40.039217,116.405748 40.0393,116.407467 40.040663,116.408891 40.043285,116.407772 40.043732,116.407806 40.044526,116.407572 40.044526,116.407451 40.044531,116.407294 40.044552,116.406973 40.044591,116.406609 40.044639,116.406574 40.044891,116.406548 40.045182,116.406513 40.045954,116.406461 40.046627,116.406383 40.047317,116.406323 40.047933,116.406262 40.048428,116.406192 40.049044,116.406132 40.049761,116.407026 40.049782,116.407182 40.049665,116.407694 40.049639,116.408345 40.049618,116.408449 40.049618,116.408302 40.050312,116.408128 40.051163,116.407868 40.052274,116.407781 40.052682,116.408441 40.052947,116.409679 40.053566,116.409698 40.053588,116.409709 40.053603,116.409795 40.053624,116.409674 40.054735,116.409612 40.054801,116.409662 40.054856,116.409587 40.055621,116.410499 40.055716,116.41174 40.055838,116.412833 40.055928,116.414252 40.05597,116.414453 40.056036,116.414831 40.056042,116.414898 40.055989,116.415508 40.056003,116.415563 40.056018,116.41561 40.056006,116.415914 40.056466,116.416304 40.056383,116.416625 40.056383,116.416886 40.056513,116.41758 40.056804,116.417935 40.05696,116.418889 40.057398,116.419427 40.057641,116.419687 40.057758,116.420269 40.058022,116.420789 40.058209,116.420989 40.058343,116.421146 40.058404,116.42222 40.058697,116.422361 40.058742,116.423579 40.05934,116.424861 40.05977,116.426375 40.060191,116.4331 40.062234,116.433276 40.062283,116.433944 40.06233,116.43429 40.062334,116.434901 40.062293,116.434934 40.062287,116.434957 40.06229,116.434949 40.062164,116.435348 40.062146,116.435634 40.062116,116.436223 40.062076,116.436881 40.062036,116.437297 40.062009,116.437653 40.061965,116.437904 40.061917,116.437766 40.061297,116.44286 40.061329,116.442912 40.060821,116.444385 40.060784,116.44461 40.060675,116.444844 40.060527,116.445147 40.060353,116.445459 40.060179,116.445744 40.060013,116.445996 40.059878,116.446264 40.059752,116.446593 40.059617,116.446931 40.059491,116.447191 40.059395,116.447295 40.05936,116.447416 40.05932,116.447659 40.059246,116.44791 40.059176,116.448187 40.059106,116.448456 40.059036,116.448715 40.058971,116.448958 40.058901,116.449174 40.058844,116.449348 40.058796,116.449503 40.058774,116.449676 40.058765,116.449928 40.058765,116.450266 40.058769,116.450664 40.058768,116.451053 40.058763,116.451348 40.058753,116.451539 40.058744,116.451625 40.058761,116.451634 40.058909,116.451616 40.059278,116.451573 40.059816,116.451513 40.060429,116.45147 40.060928,116.451444 40.061171,116.451426 40.061297,116.453747 40.061748,116.454145 40.061951,116.454197 40.061886,116.454249 40.061808,116.454292 40.061704,116.454318 40.061604,116.454336 40.061521,116.454379 40.061465,116.454457 40.061426,116.454604 40.061382,116.454803 40.06133,116.455054 40.061268,116.455288 40.061211,116.455461 40.061176,116.455582 40.061168,116.45566 40.061184,116.455729 40.061236,116.455799 40.061319,116.455868 40.061406,116.455938 40.06147,116.455989 40.061505,116.456024 40.061509,116.456041 40.061483,116.45605 40.061431,116.456067 40.061366,116.456093 40.061301,116.456145 40.061249,116.456214 40.061214,116.456318 40.061192,116.456439 40.061179,116.456569 40.061174,116.456691 40.061182,116.456812 40.061217,116.456959 40.06129,116.457089 40.061364,116.457201 40.061407,116.457305 40.061424,116.457427 40.061424,116.457548 40.061406,116.457669 40.061371,116.457755 40.061336,116.457799 40.061297,116.457799 40.061241,116.457773 40.061154,116.457729 40.061029,116.457678 40.060864,116.457626 40.060699,116.457574 40.060582,116.457539 40.060491,116.457522 40.060412,116.457513 40.060343,116.457513 40.060287,116.457539 40.060239,116.457608 40.060186,116.457729 40.060134,116.457903 40.060082,116.458119 40.060042,116.458353 40.06002,116.458621 40.060006,116.458941 40.059997,116.459218 40.059988,116.4594 40.059991,116.45953 40.060018,116.459686 40.060091,116.459859 40.060212,116.460041 40.060385,116.460223 40.060585,116.46037 40.060775,116.460474 40.060931,116.460517 40.061044,116.460534 40.061179,116.460534 40.061329,116.460514 40.061367,116.460534 40.0614,116.460534 40.061647,116.460534 40.061864,116.460534 40.062056,116.460543 40.062238,116.46056 40.062394,116.460595 40.062524,116.460629 40.062624,116.460664 40.062706,116.460707 40.06278,116.460768 40.062836,116.460872 40.062875,116.461019 40.062896,116.461192 40.062909,116.461331 40.062926,116.461452 40.062952,116.461547 40.062991,116.461616 40.063047,116.46166 40.063121,116.461686 40.063212,116.461694 40.063312,116.461686 40.06342,116.461651 40.06352,116.461608 40.063607,116.461547 40.063672,116.461461 40.063712,116.461322 40.063729,116.461123 40.063734,116.460864 40.063726,116.460604 40.063718,116.460379 40.063718,116.460223 40.063727,116.460127 40.063758,116.460076 40.063806,116.46005 40.063871,116.460041 40.063958,116.460076 40.064066,116.460136 40.064179,116.460214 40.064274,116.460301 40.064361,116.460396 40.064447,116.460491 40.06453,116.460604 40.064603,116.460734 40.064673,116.460872 40.06475,116.461011 40.064833,116.461132 40.064915,116.461236 40.065001,116.461322 40.065092,116.4614 40.065196,116.461496 40.065317,116.461591 40.065452,116.461669 40.065599,116.461747 40.065738,116.461807 40.065863,116.461851 40.065972,116.461859 40.066068,116.461842 40.066159,116.461799 40.066259,116.461747 40.066363,116.461686 40.066471,116.461643 40.066572,116.461617 40.066654,116.461617 40.066732,116.461643 40.066819,116.461686 40.06691,116.461738 40.067001,116.461807 40.067083,116.461894 40.067153,116.461989 40.067213,116.462067 40.067265,116.462119 40.067313,116.462145 40.067361,116.462145 40.067408,116.462128 40.067456,116.462102 40.067499,116.462076 40.067543,116.462041 40.067586,116.461989 40.067626,116.46192 40.067682,116.461833 40.067748,116.461756 40.067817,116.461686 40.067869,116.461617 40.067904,116.461531 40.067926,116.461435 40.067939,116.461331 40.067935,116.461219 40.067914,116.461089 40.067888,116.460959 40.067854,116.460847 40.067824,116.460769 40.067806,116.460717 40.067806,116.460691 40.067819,116.460665 40.067837,116.460648 40.067889,116.460639 40.067984,116.46063 40.06811,116.460622 40.068245,116.460604 40.068384,116.460579 40.068527,116.460561 40.06867,116.460535 40.068814,116.460509 40.068931,116.460492 40.069027,116.460458 40.069105,116.460397 40.069196,116.460336 40.069287,116.460276 40.06937,116.460224 40.069452,116.460198 40.069535,116.460172 40.069613,116.460154 40.069683,116.460146 40.069769,116.460128 40.069896,116.460111 40.07003,116.460077 40.07013,116.459999 40.070204,116.459851 40.070261,116.459627 40.070309,116.459341 40.070336,116.459047 40.070354,116.458821 40.070363,116.45864 40.070376,116.458814 40.075793,116.459177 40.075853,116.459575 40.075908,116.459922 40.07596,116.460216 40.076007,116.460502 40.076058,116.46077 40.07611,116.461047 40.076157,116.461315 40.076208,116.461592 40.076273,116.461817 40.076333,116.461973 40.076376,116.462103 40.076411,116.462233 40.076462,116.462362 40.076527,116.462475 40.076601,116.462553 40.076674,116.462596 40.076735,116.462605 40.076787,116.462596 40.076848,116.462544 40.076939,116.462432 40.077087,116.462302 40.077244,116.462233 40.077352,116.462189 40.077431,116.462155 40.0775,116.46212 40.077582,116.462095 40.077696,116.46206 40.077813,116.462025 40.077921,116.461973 40.078017,116.461913 40.078113,116.461861 40.078186,116.461826 40.078234,116.461809 40.078269,116.461792 40.078313,116.4618 40.078391,116.461817 40.078525,116.461844 40.078677,116.46187 40.07882,116.461904 40.078955,116.461956 40.079094,116.462025 40.079224,116.462103 40.079332,116.462207 40.079427,116.462328 40.079531,116.462441 40.079653,116.462519 40.079795,116.462545 40.079934,116.462536 40.080034,116.462484 40.080125,116.462398 40.08023,116.462277 40.080351,116.462155 40.080491,116.462069 40.080604,116.461861 40.08083,116.462199 40.081129,116.462926 40.08147,116.463653 40.081698,116.46438 40.081927,116.465107 40.082159,116.465834 40.082279,116.466241 40.082348,116.466146 40.082686,116.466033 40.083051,116.465929 40.08339,116.465826 40.083677,116.465765 40.083846,116.465704 40.08415,116.465661 40.084298,116.465705 40.084736,116.465756 40.084958,116.465886 40.085118,116.466129 40.085226,116.466492 40.085294,116.466829 40.085367,116.467184 40.085527,116.467288 40.085714,116.467288 40.085883,116.467219 40.086096,116.467236 40.086291,116.467418 40.08656,116.467436 40.086764,116.467289 40.087189,116.467176 40.087736,116.467064 40.088145,116.467003 40.088583,116.466951 40.089169,116.466839 40.090185,116.469937 40.089878,116.470603 40.089725,116.471009 40.089394,116.471615 40.087886,116.473553 40.085555,116.475076 40.084965,116.47742 40.084755,116.479781 40.084518,116.482939 40.083746,116.483267 40.083562,116.483388 40.083241,116.482938 40.082378,116.484019 40.08251,116.484504 40.082482,116.485135 40.082324,116.485698 40.082002,116.486666 40.08104,116.494181 40.08051,116.49828 40.080302,116.499335 40.080395,116.501073 40.079404,116.50301 40.078148,116.503806 40.077616,116.504584 40.076915,116.505388 40.076114,116.506261 40.074883,116.506771 40.074348,116.508431 40.073057,116.510334 40.071746,116.511398 40.071083,116.512694 40.070658,116.51394 40.070424,116.514934 40.07046,116.516577 40.070742,116.518047 40.071076,116.520175 40.0713,116.52309 40.071443,116.5245 40.071439,116.525987 40.071335,116.527146 40.071128,116.528228 40.070825,116.529413 40.070288,116.530589 40.069577,116.532959 40.067982,116.534369 40.066789,116.536428 40.064683,116.537795 40.063121,116.538721 40.062259,116.539733 40.061601,116.542173 40.059993,116.543533 40.059096,116.545225 40.060403,116.545776 40.061545,116.546008 40.062204,116.546418 40.062463,116.548041 40.06221,116.54793 40.061797,116.548196 40.061762,116.548167 40.061648,116.547889 40.061677,116.547286 40.059836,116.54838 40.058935,116.548682 40.058873,116.550936 40.059147,116.551718 40.058116,116.55132 40.056458,116.55028 40.056325,116.55191 40.053749,116.551788 40.053608,116.54999 40.052443,116.550518 40.051459,116.545605 40.049975,116.546557 40.048677,116.5476 40.047626,116.548706 40.046826,116.550749 40.045506,116.552454 40.044799,116.557926 40.042713,116.561641 40.041083,116.56424 40.039654,116.565903 40.038545,116.567124 40.037514,116.568268 40.036128,116.569966 40.033356,116.570469 40.032431,116.572713 40.033161,116.574576 40.03124,116.577688 40.029531,116.578165 40.030142,116.574785 40.03236,116.574039 40.033107,116.578807 40.033102,116.579283 40.029342,116.577809 40.027508,116.581043 40.025694,116.595547 40.017516,116.598202 40.015679,116.60131 40.013877,116.603194 40.013322,116.605182 40.012974,116.614945 40.012522,116.617978 40.012176,116.619898 40.011795,116.62168 40.011028,116.628129 40.007652,116.625756 40.003119,116.632737 39.999819,116.63759 40.002351,116.638356 40.001762,116.641165 39.998666,116.642687 39.996759,116.643261 39.995379,116.643852 39.992975,116.643991 39.99091,116.643756 39.989616,116.642904 39.989728,116.640311 39.990184,116.639476 39.987574,116.639119 39.986884,116.633657 39.986204,116.632909 39.985239,116.63403 39.981703,116.639823 39.982609,116.640119 39.980561,116.641023 39.980432,116.640623 39.978804,116.640318 39.976585,116.640727 39.9735,116.641222 39.97163,116.641822 39.969582,116.642031 39.968058,116.6425 39.963844,116.642882 39.95868,116.64309 39.952986,116.643769 39.950934,116.644986 39.948266,116.645212 39.946808,116.645273 39.945983,116.644525 39.946186,116.640471 39.948259,116.638131 39.949303,116.634531 39.95004,116.633398 39.950288,116.633385 39.950308,116.633209 39.950309,116.633192 39.950333,116.632796 39.95042,116.632791 39.950452,116.63262 39.950459,116.632226 39.950545,116.631513 39.948383,116.631469 39.947688,116.631208 39.947046,116.630713 39.946611,116.630486 39.946155,116.630652 39.945335,116.631973 39.942979,116.633434 39.940898,116.633608 39.939886,116.633581 39.939335,116.633173 39.939057,116.632981 39.938948,116.632399 39.938947,116.630364 39.939149,116.629816 39.939066,116.629686 39.938727,116.630147 39.937964,116.630286 39.937357,116.630286 39.937005,116.629459 39.931587,116.629302 39.931314,116.62812 39.931647,116.625729 39.932183,116.624851 39.931791,116.624147 39.929976,116.625929 39.927867,116.625997 39.927748,116.626112 39.927651,116.626823 39.92681,116.626858 39.926767,116.626836 39.926746,116.626884 39.926737,116.626954 39.926654,116.630571 39.921675,116.629814 39.921587,116.62465 39.922498,116.620964 39.923099,116.621095 39.922387,116.620373 39.922221,116.621442 39.920104,116.621929 39.919479,116.620078 39.919157,116.619851 39.918518,116.620278 39.918124,116.622442 39.918277,116.622737 39.915491,116.623015 39.913824,116.622841 39.913811,116.621693 39.913728,116.621737 39.913359,116.621016 39.913189,116.622023 39.910073,116.621684 39.909565,116.621111 39.909118,116.620928 39.908979,116.620135 39.908369,116.620092 39.908379,116.620085 39.908332,116.62025 39.907668,116.620876 39.907768,116.622666 39.907717,116.622692 39.906584,116.622386 39.906625,116.620806 39.906596,116.621101 39.90443,116.623361 39.904276,116.623379 39.903854,116.622101 39.902499,116.622088 39.90238,116.619227 39.899526,116.617801 39.899698,116.617554 39.899122,116.617482 39.898955,116.617848 39.898749,116.618677 39.898455,116.619505 39.898353,116.619956 39.897944,116.618547 39.897252,116.618076 39.897136,116.614001 39.896813,116.614001 39.896161,116.614878 39.895498,116.615313 39.895502,116.615607 39.889791,116.61685 39.8897,116.619041 39.889857,116.619084 39.889847,116.619128 39.889863,116.627584 39.890473,116.628141 39.890282,116.628142 39.890111,116.628152 39.890008,116.628143 39.889958,116.628149 39.889336,116.628714 39.884926,116.628957 39.8827,116.628983 39.881593,116.624314 39.881159,116.62494 39.877253,116.624453 39.87615,116.619993 39.868954,116.620758 39.867523,116.623 39.867637,116.6234 39.865246,116.626389 39.861919,116.625798 39.861076,116.626963 39.860683,116.625459 39.859383,116.62452 39.858576,116.623668 39.857967,116.622912 39.857397,116.61754 39.853062,116.613456 39.850178,116.609886 39.850454,116.604189 39.850071,116.604675 39.846134,116.608357 39.846534,116.607653 39.84597,116.601904 39.84073,116.601874 39.840365,116.601891 39.840309,116.601868 39.840287,116.601356 39.834063,116.60225 39.83168,116.598969 39.831659,116.599012 39.829332,116.59922 39.825591,116.598117 39.824684,116.597675 39.826151,116.59147 39.826362,116.591113 39.826027,116.591591 39.82387,116.58825 39.82441,116.585008 39.824777,116.584964 39.824795,116.584845 39.824796,116.58374 39.824921,116.58426 39.825585,116.58511 39.826372,116.585141 39.826382,116.585175 39.826433,116.585206 39.826461,116.587053 39.828182,116.587007 39.828223,116.58309 39.827943,116.57748 39.827545,116.577142 39.830675,116.574784 39.831419,116.574763 39.831448,116.574746 39.831431,116.5726 39.832109,116.572488 39.83332,116.572332 39.835043,116.569359 39.834961,116.56938 39.834643,116.56945 39.834432,116.569386 39.833505,116.567895 39.833479,116.558591 39.834693,116.557173 39.834641,116.557139 39.834637,116.557098 39.834638,116.553606 39.83451,116.553339 39.834549,116.553226 39.834496,116.551977 39.834451,116.551944 39.834444,116.551885 39.834448,116.551621 39.834438,116.551022 39.834373,116.550889 39.834399,116.548176 39.834255,116.545051 39.836042,116.543657 39.835077,116.543431 39.834551,116.543268 39.833255,116.542757 39.830616,116.542756 39.8306,116.542751 39.830587,116.542679 39.830214,116.541052 39.829549,116.538093 39.831423,116.536437 39.831433,116.536394 39.831462,116.536335 39.831434,116.535671 39.831438,116.535082 39.832864,116.533196 39.832734,116.532521 39.831074,116.536229 39.829644,116.536243 39.829634,116.536276 39.829627,116.537312 39.828845,116.53732 39.828802,116.537384 39.828791,116.538153 39.828211,116.537677 39.826676,116.534952 39.827104,116.534934 39.824825,116.531655 39.825099,116.531604 39.82664,116.52586 39.826903,116.525497 39.827017,116.525297 39.827152,116.525185 39.827404,116.525359 39.829752,116.516156 39.829827,116.516156 39.830413,116.514626 39.830417,116.514288 39.829333,116.514276 39.829301,116.514271 39.829277,116.514265 39.829271,116.514124 39.828887,116.514095 39.82881,116.513855 39.828158,116.513657 39.827729,116.510612 39.827643,116.510578 39.825481,116.510828 39.824269,116.511217 39.823513,116.510904 39.823486,116.509831 39.823334,116.509882 39.821878,116.510146 39.821445,116.508281 39.820898,116.502794 39.818998,116.502932 39.818889,116.504264 39.819124,116.505821 39.81829,116.505803 39.817864,116.502543 39.817662,116.502698 39.816433,116.49821 39.815704,116.497717 39.817069,116.495227 39.816169,116.495357 39.818799,116.493351 39.819083,116.493506 39.820241,116.492962 39.820316,116.492667 39.818537,116.492528 39.818516,116.492425 39.818564,116.490367 39.818791,116.490318 39.81879,116.490259 39.818802,116.489952 39.818836,116.489686 39.81785,116.489682 39.817818,116.489668 39.817785,116.489458 39.817006,116.48977 39.81694,116.489744 39.816645,116.485627 39.816882,116.485246 39.812725,116.476076 39.809968,116.47599 39.810263,116.475752 39.810196,116.475751 39.81015,116.475248 39.810016,116.474922 39.809931,116.474896 39.809952,116.474251 39.809768,116.469051 39.814244,116.468757 39.814457,116.468463 39.814519,116.463322 39.814344,116.463106 39.815243,116.462769 39.815938,116.462301 39.816473,116.461799 39.816904,116.458753 39.819211,116.453992 39.822637,116.453749 39.822134,116.453134 39.822192,116.452736 39.823017,116.44391 39.820953,116.443589 39.821206,116.442931 39.822639,116.442854 39.823169,116.443313 39.824128,116.443789 39.824851,116.444231 39.825854,116.444552 39.826213,116.444994 39.826334,116.445713 39.826172,116.445912 39.826693,116.444873 39.827341,116.443166 39.827947,116.442603 39.828239,116.441511 39.829126,116.441443 39.828452,116.44144 39.828437,116.441439 39.828421,116.441338 39.827425,116.436676 39.827431,116.436711 39.827731,116.437006 39.828668,116.437006 39.82918,116.43698 39.830656,116.430063 39.830116,116.430488 39.83187,116.4286 39.831879,116.428545 39.831865,116.428503 39.83188,116.428122 39.831869,116.428075 39.831882,116.425216 39.831896,116.425182 39.832721,116.431225 39.832685,116.431303 39.832919,116.432057 39.832931,116.432082 39.834284,116.432084 39.834314,116.432084 39.834369,116.432092 39.834828,116.432629 39.834663,116.433237 39.837206,116.433599 39.837129,116.433634 39.837087,116.433677 39.83716,116.434034 39.837778,116.43497 39.837564,116.435204 39.83815,116.436114 39.838036,116.436791 39.839233,116.435967 39.839677,116.436426 39.840405,116.436323 39.840631,116.436401 39.840753,116.43673 39.841334,116.437475 39.840947,116.437354 39.840721,116.438307 39.840268,116.438524 39.839882,116.438558 39.839352,116.438731 39.839352,116.43874 39.839699,116.440586 39.839649,116.441314 39.840946,116.441851 39.840927,116.441868 39.841284,116.442328 39.841266,116.442326 39.841521,116.442269 39.841627,116.442326 39.84173,116.442319 39.843679,116.443549 39.843733,116.443584 39.842952,116.443047 39.842953,116.443029 39.842493,116.443809 39.842492,116.444043 39.844284,116.446703 39.844262,116.445976 39.848335,116.446107 39.848336,116.446128 39.848336,116.446152 39.848336,116.446311 39.848337,116.446326 39.848336,116.446341 39.848337,116.448037 39.848349,116.448054 39.848253,116.448513 39.848253,116.448513 39.848352,116.449353 39.848359,116.44931 39.848703,116.450488 39.848696,116.450488 39.849217,116.450228 39.849222,116.450228 39.849999,116.451804 39.850087,116.451813 39.850495,116.451605 39.850569,116.451753 39.851433,116.451094 39.851534,116.451138 39.852015,116.453433 39.851963,116.453424 39.851594,116.453632 39.851586,116.453883 39.851081,116.45506 39.850593,116.456134 39.850582,116.456134 39.850144,116.456506 39.850151,116.456532 39.850629,116.457961 39.850694,116.458012 39.850627,116.45806 39.850563,116.458012 39.849137,116.458063 39.849104,116.458089 39.849084,116.458113 39.849072,116.458809 39.848619,116.460306 39.848616,116.460358 39.849379,116.460869 39.849365,116.460964 39.850845,116.461691 39.850809,116.462384 39.850773,116.463093 39.850233,116.463085 39.850684,116.463665 39.850696,116.463665 39.851742,116.465093 39.851717,116.465682 39.853417,116.467715 39.852592,116.467794 39.856008,116.467093 39.856504,116.464748 39.856549,116.464612 39.856484,116.464544 39.856477,116.464514 39.856436,116.464592 39.85625,116.463311 39.856227,116.463136 39.856368,116.463105 39.8564,116.463073 39.85642,116.461886 39.857387,116.461794 39.857424,116.461794 39.857461,116.461493 39.857707,116.461518 39.858175,116.461521 39.858192,116.461522 39.858235,116.461562 39.858991,116.460792 39.859023,116.460801 39.858047,116.460558 39.857769,116.456066 39.861221,116.456066 39.859684,116.455555 39.859642,116.455555 39.859338,116.454222 39.85938,116.453676 39.859741,116.453356 39.860128,116.452672 39.860841,116.452306 39.861154,116.45225 39.861197,116.451538 39.861811,116.450949 39.862299,116.45023 39.862721,116.448869 39.863479,116.448459 39.863536,116.448467 39.862227,116.446049 39.862211,116.446066 39.861335,116.445192 39.861368,116.445129 39.862612,116.445103 39.864292,116.444309 39.864729,116.444277 39.86473,116.444258 39.864757,116.444193 39.864793,116.444167 39.865444,116.444237 39.865444,116.444252 39.866007,116.442966 39.86601,116.442963 39.866088,116.44292 39.86789,116.443024 39.869005,116.44299 39.869413,116.442842 39.869635,116.442981 39.870359,116.443276 39.870719,116.443952 39.87064,116.444783 39.870539,116.444852 39.871537,116.444861 39.871607,116.444931 39.872617,116.444047 39.872827,116.44364 39.872846)),((116.603686 40.05295,116.600429 40.052634,116.600273 40.052369,116.599457 40.052261,116.599283 40.052538,116.598519 40.052539,116.598771 40.050412,116.599344 40.049791,116.601644 40.047664,116.599413 40.047174,116.599552 40.045576,116.59989 40.042447,116.59982 40.041405,116.597911 40.042331,116.595585 40.042947,116.594673 40.043346,116.592702 40.043229,116.591983 40.043126,116.590779 40.043388,116.590775 40.043543,116.590733 40.043634,116.590692 40.043894,116.590639 40.043959,116.590558 40.044406,116.590397 40.044549,116.590387 40.045908,116.590495 40.046039,116.590462 40.046763,116.590396 40.046852,116.590398 40.047353,116.590398 40.048089,116.590447 40.048108,116.590456 40.04827,116.590443 40.048456,116.590398 40.048506,116.590398 40.048699,116.591198 40.048644,116.591764 40.048635,116.591759 40.048761,116.59142 40.050449,116.591204 40.051791,116.589512 40.049816,116.587959 40.050538,116.588248 40.050793,116.588277 40.050902,116.588286 40.050932,116.588354 40.051015,116.589451 40.052608,116.589868 40.053809,116.590093 40.055008,116.590128 40.056162,116.590007 40.057243,116.589617 40.059656,116.589071 40.059605,116.58894 40.059583,116.588411 40.059583,116.588255 40.059583,116.588071 40.059586,116.587986 40.059588,116.587449 40.060248,116.587431 40.06027,116.587498 40.060284,116.587554 40.060297,116.587801 40.060338,116.587894 40.060371,116.588281 40.060456,116.588619 40.060699,116.588715 40.060868,116.588429 40.063217,116.586591 40.074329,116.584987 40.07414,116.583712 40.074015,116.582506 40.073908,116.581388 40.073822,116.581578 40.072485,116.581249 40.072446,116.581301 40.072016,116.581457 40.070923,116.581595 40.069889,116.581092 40.069838,116.581413 40.067849,116.580728 40.067807,116.580711 40.067997,116.580607 40.068757,116.580563 40.069257,116.580468 40.069803,116.580069 40.069782,116.580052 40.069861,116.579939 40.070707,116.579792 40.071675,116.579697 40.072339,116.578778 40.072266,116.578648 40.073113,116.578483 40.074229,116.578327 40.075153,116.578197 40.076117,116.578143 40.076463,116.578033 40.077176,116.577859 40.078253,116.577712 40.079195,116.577556 40.080254,116.577305 40.081379,116.577296 40.081947,116.577141 40.082503,116.576984 40.083072,116.576976 40.083632,116.576967 40.084196,116.576886 40.084396,116.576843 40.084435,116.576673 40.084766,116.576664 40.085321,116.576508 40.08589,116.576352 40.08645,116.576222 40.086921,116.576257 40.086966,116.576196 40.087014,116.576196 40.087574,116.57604 40.088139,116.576032 40.088707,116.576023 40.089267,116.575867 40.089832,116.575711 40.090392,116.575703 40.090961,116.575555 40.091516,116.575399 40.092085,116.575252 40.092641,116.575243 40.092867,116.574316 40.096132,116.574464 40.096358,116.574455 40.096701,116.574455 40.097048,116.574377 40.099674,116.573832 40.103338,116.573485 40.105179,116.574465 40.105299,116.574075 40.107809,116.574404 40.107834,116.575271 40.107911,116.575532 40.107933,116.57567 40.107078,116.575921 40.105432,116.576302 40.105462,116.576519 40.104311,116.577403 40.104293,116.577507 40.104176,116.577559 40.103681,116.577715 40.103303,116.578313 40.102734,116.580289 40.088968,116.580366 40.088356,116.586697 40.088949,116.591816 40.089414,116.593369 40.089574,116.593195 40.089961,116.595894 40.09022,116.59579 40.091001,116.596042 40.091678,116.59586 40.093215,116.598707 40.093514,116.598221 40.096613,116.597779 40.099326,116.599046 40.099391,116.59876 40.100849,116.598448 40.101275,116.598083 40.10177,116.597944 40.102282,116.597944 40.102672,116.59791 40.102976,116.597814 40.10315,116.59771 40.103393,116.598387 40.10387,116.599333 40.101118,116.599923 40.099434,116.600652 40.099473,116.60152 40.093787,116.602909 40.093878,116.603256 40.09166,116.603577 40.089572,116.603829 40.087922,116.604601 40.087987,116.604723 40.087254,116.604471 40.087241,116.604523 40.086881,116.603464 40.086811,116.603837 40.084307,116.604045 40.083061,116.604167 40.082301,116.60434 40.081186,116.604548 40.079871,116.604696 40.078951,116.604895 40.077679,116.605034 40.076776,116.605182 40.075791,116.605398 40.074402,116.60552 40.073612,116.606666 40.073768,116.606771 40.073069,116.606084 40.073048,116.605615 40.073017,116.605806 40.071832,116.605971 40.070786,116.606032 40.070426,116.606197 40.069328,116.606127 40.069319,116.60644 40.067409,116.606726 40.065521,116.607047 40.063442,116.607351 40.061446,116.608115 40.061415,116.608115 40.060781,116.607542 40.060799,116.607568 40.060578,116.607628 40.060134,116.607776 40.059201,116.607915 40.058385,116.608019 40.057708,116.608158 40.056818,116.608288 40.056046,116.608392 40.055364,116.608401 40.054909,116.608288 40.054601,116.607854 40.05365,116.607732 40.053359,116.607636 40.053324,116.606942 40.053254,116.606967 40.053016,116.60702 40.052551,116.60682 40.05253,116.606403 40.052482,116.606143 40.052465,116.606004 40.053159,116.603781 40.052959,116.603686 40.05295)))";
        System.out.println(simplifyWktFunc(wkt, null));
        wkt = GeoUtil.geoJSONtoWkt(simplifyMultiPolygonWktFunc(GeoUtil.wktToGeoJSON(wkt)));
        System.out.println(wkt);
        System.out.println("result wkt length: " + wkt.split(",").length);
    }

}
